import Vue from 'vue';
import store from './store';
import VueRouter from 'vue-router';
import VueResource from 'vue-resource';
require('vue-swipe/dist/vue-swipe.css');
import { Swipe, SwipeItem } from 'vue-swipe';
import App from './App';

/* 载入模板 */
import BMapComponent from 'components/BMapComponent/BMapComponent';
import appointment from 'components/appointment/appointment';
import changepasswd from 'components/changepasswd/changepasswd';
import choseclass from 'components/choseclass/choseclass';
import classdetail from 'components/classdetail/classdetail';
import coaches from 'components/coaches/coaches';
import coachconfirm from 'components/coachconfirm/coachconfirm';
import comment from 'components/comment/comment';
import finish from 'components/finish/finish';
import index from 'components/index/index';
import login from 'components/login/login';
import myself from 'components/myself/myself';
import myorders from 'components/myorders/myorders';
import notice from 'components/notice/notice';
import order from 'components/order/order';
import ordercancel from 'components/ordercancel/ordercancel';
import orderdetail from 'components/orderdetail/orderdetail';
import personal from 'components/personal/personal';
import register from 'components/register/register';
import stores from 'components/stores/stores';
import storedetail from 'components/storedetail/storedetail';
import Dialog from 'hsy-vue-dialog';
import 'common/stylus/color.styl';
import 'common/stylus/index.styl';

Vue.use(VueRouter);
Vue.use(VueResource);
Vue.use(Dialog);
Vue.component('swipe', Swipe);
Vue.component('swipe-item', SwipeItem);

/* 载入跳转路径 */
const routes = [
/*  { path: '*',
    component: NotFoundComponent
  }, */
  {
    path: '/',
    component: index
  },
  {
    path: '/BMapComponent/',
    component: BMapComponent
  },
  {
    path: '/classDetail/:id',
    component: classdetail
  },
  {
    path: '/order/:id',
    component: order
  },
  {
    path: '/finish/',
    component: finish
  },
  {
    path: '/choseclass/:id',
    component: choseclass
  },
  {
    path: '/notice/',
    component: notice
  },
  {
    path: '/stores/:id',
    component: stores
  },
  {
    path: '/storedetail/:id',
    component: storedetail
  },
  {
    path: '/appointment/',
    component: appointment
  },
  {
    path: '/coaches/:id',
    component: coaches
  },
  {
    path: '/coachconfirm/',
    component: coachconfirm
  },
  {
    path: '/comment/',
    component: comment
  },
  {
    path: '/myself/',
    component: myself
  },
  {
    path: '/register/',
    component: register
  },
  {
    path: '/login/',
    component: login
  },
  {
    path: '/changepasswd/',
    component: changepasswd
  },
  {
    path: '/myorders/',
    component: myorders
  },
  {
    path: '/personal/',
    component: personal
  },
  {
    path: '/ordercancel/',
    component: ordercancel
  },
  {
    path: '/orderdetail/',
    component: orderdetail
  }
];

const router = new VueRouter({
  mode: 'history',
  linkActiveClass: 'active',
  routes
});

// 登陆跳转模块
const routeMatch = ['appoint', 'myself', 'coaches', 'coachconfirm', 'comment'];
router.beforeEach((to, from, next) => {
  let nowpath = to.fullPath.split('?')[0];
  let bmatch = routeMatch.filter(value => nowpath.indexOf(value) > -1);
  if (bmatch.length !== 0 && !window.localStorage.cphone) {
    next({path: '/login/?redireat_url=' + escape(to.fullPath)});
  } else {
    next();
  }
});

/* eslint-disable no-new */
Vue.http.options.root = 'http://cj.lightway.cc';
Vue.http.options.emulateJSON = true;
new Vue({
  el: '#app',
  store,
  router,
  render: h => h(App)
});
Vue.directive('title', {
  inserted: function (el, binding) {
    document.title = el.innerText;
  }
});
